package com.duyutian.controller.manageController;

import com.duyutian.common.Ueditor;
import com.duyutian.util.OSSClientUtil;
import com.jfinal.json.JFinalJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by dyt on 2018/9/27.
 */
@Controller
public class UediteController {
    private Logger logger = LoggerFactory.getLogger(UediteController.class);


    /*kindedit测试*/
   /* @RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartHttpServletRequest request) throws IOException {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        //得到所有的图片
        Collection<MultipartFile> values = fileMap.values();
        Iterator<MultipartFile> iterator = values.iterator();
        //迭代器遍历
        while (iterator.hasNext()){
            //文件上传
            MultipartFile next = iterator.next();
            logger.info("图片大小为:"+next.getSize());
            //图像名称
           String fileName =  next.getOriginalFilename();
           //设置图片存储路径
            return JFinalJson.getJson().toJson(KindEditMsg.success(UploadUtil.upload(next)));
        }
        return "";
    }*/

    @RequestMapping("/uupload")
    @ResponseBody
    public String uupload(@RequestParam("action")String action, MultipartFile upfile) throws IOException {
        if(action!=null&&action.equals("config")){
            return Ueditor.UEDITOR_CONFIG;
        }else if(action !=null && action.equals("uploadimage")){
            //图片上传
            Ueditor ueditor = new Ueditor();

            OSSClientUtil articleOss = OSSClientUtil.init("article/");
            String url = articleOss.uploadImg2Oss(upfile);
            ueditor.setUrl(articleOss.getImgUrl(url));
            ueditor.setOriginal(upfile.getOriginalFilename());
            ueditor.setState("SUCCESS");
            ueditor.setTitle("");
            return JFinalJson.getJson().toJson(ueditor);
        }
        return "";
    }


}
